package com.uinnova.product.eam.service.impl;

import com.binary.jdbc.Page;
import com.uinnova.project.base.diagram.comm.model.ESDiagram;
import com.uinnova.project.base.diagram.comm.model.ESDiagramLink;
import com.uinnova.project.db.eam.ESDiagramLinkDao;
import com.uino.bean.cmdb.base.ESCIRltInfo;
import org.apache.commons.lang3.StringUtils;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.CountDownLatch;
import java.util.stream.Collectors;

public class CiRltTask implements Runnable{

    private  ConcurrentLinkedQueue<List<String>> allCiCodeQueue;
    private List<ESCIRltInfo> returnVals;
    private List<ESDiagram> diagramList;
    private ESDiagramLinkDao esDiagramLinkDao;

    private CountDownLatch countDownLatch;

    public CiRltTask(ConcurrentLinkedQueue<List<String>> allCiCodeQueue, List<ESCIRltInfo> returnVals, List<ESDiagram> diagramList, ESDiagramLinkDao esDiagramLinkDao, CountDownLatch countDownLatch) {
        this.allCiCodeQueue = allCiCodeQueue;
        this.returnVals = returnVals;
        this.diagramList = diagramList;
        this.esDiagramLinkDao = esDiagramLinkDao;
        this.countDownLatch = countDownLatch;
    }


    @Override
    public void run() {
        Set<Long> diagramIdSet = diagramList.stream().map(ESDiagram::getId).collect(Collectors.toSet());
        BoolQueryBuilder boolQueryLinkBuilder = QueryBuilders.boolQuery();
        boolQueryLinkBuilder.must(QueryBuilders.termsQuery("diagramId",diagramIdSet));
        long count = esDiagramLinkDao.countByCondition(boolQueryLinkBuilder);
        List<ESDiagramLink> esDiagramLinks = new ArrayList<>();
        try {
            if (count > 0) {
                Map<String, Page<ESDiagramLink>> linkMap = esDiagramLinkDao.getScrollByQuery(1, 3000, boolQueryLinkBuilder, "id", true);
                String scrollId = linkMap.keySet().iterator().next();
                if (linkMap.get(scrollId).getData() != null && linkMap.get(scrollId).getData().size() > 0) {
                    List<ESDiagramLink> firstData = linkMap.get(scrollId).getData();
                    esDiagramLinks.addAll(firstData);
                }
                if (linkMap.get(scrollId).getTotalRows() > 3000) {
                    while (true) {
                        List<ESDiagramLink> rltNextResult = esDiagramLinkDao.getListByScroll(scrollId);
                        if (rltNextResult != null && rltNextResult.size() > 0) {
                            esDiagramLinks.addAll(rltNextResult);
                        } else {
                            break;
                        }
                    }
                }
                List<String> rltUniqueCodes = esDiagramLinks.stream().map(ESDiagramLink::getUniqueCode)
                        .filter(item -> StringUtils.isNotEmpty(item))
                        .distinct().collect(Collectors.toList());
                List<String> findRltCode = returnVals.stream().filter(item -> rltUniqueCodes.contains(item.getUniqueCode())).map(item -> item.getUniqueCode()).distinct().collect(Collectors.toList());
                if (!CollectionUtils.isEmpty(findRltCode)) {
                    allCiCodeQueue.add(findRltCode);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            countDownLatch.countDown();
        }
    }
}
